package com.sleepfish.generator.controller.from;

import com.sleepfish.common.core.controller.BaseController;
import com.sleepfish.common.core.domain.AjaxResult;
import com.sleepfish.generator.domain.BpmForm;
import com.sleepfish.generator.domain.VO.form.*;
import com.sleepfish.generator.service.IBpmFormService;
import com.sleepfish.generator.service.IBpmTableService;
import com.sleepfish.generator.util.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "动态表格")
@RestController
@RequestMapping("/bpm/table")
public class BpmTableController extends BaseController {

    @Resource
    private IBpmTableService TableService;


    @PostMapping("/create")
    @ApiOperation("创建动态表格")
    //@PreAuthorize("@ss.hasPermi('bpm:form:create')")
    public AjaxResult createForm(@Valid @RequestBody BpmFormCreateReqVO createReqVO) {

        return AjaxResult.success(TableService.createForm(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新动态表格")
    //@PreAuthorize("@ss.hasPermi('bpm:form:update')")
    public AjaxResult updateForm(@Valid @RequestBody BpmFormUpdateReqVO updateReqVO) {
        TableService.updateForm(updateReqVO);
        return AjaxResult.success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除动态表格")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    //@PreAuthorize("@ss.hasPermi('bpm:form:delete')")
    public AjaxResult deleteForm(@RequestParam("id") Long id) {
        TableService.deleteForm(id);
        return AjaxResult.success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得动态表格")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    //@PreAuthorize("@ss.hasPermi('bpm:form:query')")
    public AjaxResult getForm(@RequestParam("id") Long id) {
        BpmForm form = TableService.getForm(id);
        BpmFormRespVO bpmFormRespVO =new BpmFormRespVO();
        BeanUtils.copyBeanProp(bpmFormRespVO,form);

        return AjaxResult.success(bpmFormRespVO);
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获得动态表格的精简列表", notes = "用于表格下拉框")
    public AjaxResult getSimpleForms() {
        List<BpmForm> list = TableService.getFormList();
        List<BpmFormSimpleRespVO> formList=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            formList.add(new BpmFormSimpleRespVO());
             BeanUtils.copyBeanProp(formList.get(i),list.get(i));
        }
        return AjaxResult.success(formList);
    }

    @GetMapping("/page")
    @ApiOperation("获得动态表格分页")
    //@PreAuthorize("@ss.hasPermi('bpm:form:query')")
    public AjaxResult getFormPage(@Valid BpmFormPageReqVO pageVO) {
        startPage();
        BpmForm bpmForm = new BpmForm();
        bpmForm.setName(pageVO.getName());
        List<BpmForm> list = TableService.getFormListVO(bpmForm);
        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        map.put("total",list.size());
        return AjaxResult.success(map);
    }

}
